//Integer currPage,Integer pageSize,String memberId,String queryStatus
var member;
var currPage=0;
var pageSize=5;
var queryStatus=$("#queryStatus").val();
//alert(queryStatus)
var memberId;
$(function(){
		member=getEntity("memberInfo");
		if(member){
			$("#mImg").attr("src",rootPath + member.mImg);
			memberId=member.mId;
		}
	   $.ajax({
			type:"post",
			url: rootPath+"/order/orderList",
			data:{'pageSize':pageSize,
				  'currPage':currPage,
				  'queryStatus': queryStatus,
				  'memberId':memberId,
			},
			/*data:{'queryStatus':queryStatus,
				  'orderStatus':orderStatus,
				  'commentStatus':commentStatus,
				  'pageSize':pageSize,
				  'currPage':currPage,
			},*/
			//dataType:"json",
			success:function(data){
				addClass(queryStatus);
				$("#orders_list").empty();
				var orderList=data.data;
				if(orderList!=null && orderList.length>0){
					var optionHtml = optionByHtml(orderList);
					optionHtml +=" <ul class='orderPageRefresh' onclick='clickRefresh"+'("'+queryStatus+'")'+";' class='shoppingcart_goods myGoods obligation' style='height: 40px;text-align: center;font-size:20px;'>点击加载更多</ul>";
					$("#orders_list").append(optionHtml);
				}
			}
		});
});

var currByPage = 2;
/***刷新分页调用***********/
function clickRefresh(queryStatus){
	addClass(queryStatus);
	 $.ajax({
			type:"post",
			url: rootPath+"/order/orderList",
			data:{'pageSize':pageSize,
				  'currPage':currByPage,
				  'queryStatus':queryStatus,
				  'memberId':memberId,
			},
			//dataType:"json",
			success:function(data){
				var orderList=data.data;
				if(orderList!=null && orderList.length>0){
					currByPage++;
					var optionHtml = optionByHtml(orderList);
					$(".orderPageRefresh").before(optionHtml);
				}else{
					$(".orderPageRefresh").text("数据加载完毕");
				}
				
			}
		});
}

/***组装订单html**************/
function optionByHtml(orderList){
	var optionHtml="";
	$.each(orderList,function(i,item){
		var productList=item.products;
		var $orderId = item.orderId;
		optionHtml+="	<ul class='shoppingcart_goods myGoods obligation'> "
				+"  <li> "
					+"  <div class='shoppingcart_store'> "
					//+"  	<a href='store.html'> "
					+"  	<a href='#'> "
					+"  		<img class='storeImg' src='"+rootPath+"/image/dian.png' alt='' /> "
					//+"  		<span>订单号："+$orderId.substr(0,14)+'...'+"</span> "
					+"  		<span>" + item.shopName + "</span> "
				//	+"  		<img src='"+rootPath+"/image/right.png' alt='' class='centerRight' /> "
					+"  		<span style='padding-left: 45%;'>" 
						optionHtml+= createByBtn(item,productList,1);
					optionHtml+=		"</span> "
					+"  	</a> "
					+"  </div> "
					if(productList.length > 0){
						optionHtml+=createGoodsHtml(item,productList);
					}
					var  price0101=parseFloat(item.totalAmount);
					var  shipMoney=parseFloat(item.shipMoney);
				 optionHtml+=" </li> "
					+" <li class='ordersWord'> "
					+" 		<p> "
					+" 			<span>共"+item.itemnum+"件商品 &nbsp;</span> "
					+"   		<span>合计:  <b class='red'>￥"+price0101.toFixed(2) +" </b> (含运费 <b class='red'>￥ "+shipMoney.toFixed(2)+"</b>)</span> "
					+" 		</p> "
					+" </li> "
					+" <li class='ordersBtn'> "
						optionHtml+= createByBtn(item,productList,2);
					/*+" 		<p> "
					+" 			<a href='' class='delO'>删除订单</a> "
					+" 	    	<a href='evaluate.html'>评价</a> "
					+" 		</p> "*/
		optionHtml+=" </li> "
			+" </ul> ";
	});
	return optionHtml;
}


/***根据订单类型选择****/
function orderByType(queryStatus){
	addClass(queryStatus);
	  $.ajax({
			type:"post",
			url: rootPath+"/order/orderList",
			data:{'pageSize':pageSize,
				  'currPage':currPage,
				  'queryStatus':queryStatus,
				  'memberId':memberId,
			},
			//dataType:"json",
			success:function(data){
				$("#orders_list").empty();
				var orderList=data.data;
				if(orderList!=null && orderList.length>0){
					var optionHtml = optionByHtml(orderList);
					optionHtml +=" <ul class='orderPageRefresh' onclick='clickRefresh"+'("'+queryStatus+'")'+";' class='shoppingcart_goods myGoods obligation' style='height: 40px;text-align: center;font-size:20px;'>点击加载更多</ul>";
					$("#orders_list").append(optionHtml);
				}
			}
		});
}




//确认收货
function  confirmReceiptOrder($orderId){
	var url =  rootPath + "/order/confirmReceiptOrder";
	//layer.confirm("要确认收货吗？",{skin: 'layui-layer-red ',offset: ['40%', '40%']},function(index){
	if(confirm("要确认收货吗？")){
		var result = CommnUtil.ajax(url,
				{"orderId":$orderId},
				"json");
		if (result.resultSuccess) {
			alert("确认收货成功!")
			reloadOrderList('4'); //待评价页面
		} else {
			alert("确认收货失败")
		    window.location.reload();
		}
	}else{
			return false;
	}	
}

//刷新订单列表
function reloadOrderList(queryStatus){
	window.location.href=rootPath + '/order/myOrders?queryStatus='+queryStatus;
}


//删除或取消
function cancelOrDeleteOrder($orderId,$status){ //订单取消订单 status 0:取消订单 ;1：删除订单
	//alert($orderId +":"+ $status)
	var url =  rootPath + "/order/cancelOrDeleteOrder";
	var confirmInfo = $status == 0 ?"确认要取消订单吗？":"确认要删除订单吗？";
    var alarmInfoSuccess = $status == 0 ?"订单取消成功!":"订单删除成功!";
    var alarmInfoFail = $status == 0 ?"订单取消失败!":"订单删除失败!";
    if(confirm(confirmInfo)){
    	var result = CommnUtil.ajax(url,
				{"orderId":$orderId,
			    "status":$status},
				"json");
		if(result.resultSuccess){
			alert(alarmInfoSuccess);
			reloadOrderList('0'); //全部订单页面
		}else{
			alert(alarmInfoFail);
			return false;
		}
	}else{
		return false;
	}
}


/****创建订单商品列表*****/
function createGoodsHtml(item, goodsList){
	var payStatus = item.payStatus;
	var shipStatus = item.shipStatus;
	var isComment = item.isComment;
	var isValid = item.isValid;
	var optionHtml = "";
	$.each(goodsList,function(i,itemIn){
		var detailUrl = rootPath + "/order/orderData?orderId="+item.orderId; //订单详情页
		var commentUrl = rootPath + "/order/evaluate?orderId="+item.orderId+"&goodsId="+itemIn.productId; //订单评价
		var addCommentUrl = rootPath + "/order/addEvaluate?orderId="+item.orderId+"&goodsId="+itemIn.productId; //订单追加评价
		var price ="";
		var isCommentCount = itemIn.isCommentCount;
		if(itemIn.discountMarketPrice){
			price = itemIn.discountMarketPrice;
		}else{
			price = itemIn.marketPrice;
		}
		var productDetailUrl = rootPath + "/goods/queryGoodsDetail?id=" + itemIn.productId;
		var goods_sku_info = '';
		if(itemIn.goods_sku_info){
			goods_sku_info = itemIn.goods_sku_info;
		}else{
			goods_sku_info ='无规格信息';
		}
		var picture  = rootPath+"/image/defualt.png";
		if(itemIn.picture){
			picture = itemIn.picture;
		}
		var price01 =parseFloat(price)
		optionHtml+="<div class='shoppingcartDetail'> "
				+" <a class='sdImg' href='"+detailUrl+"'> "
				+" <img src='"+picture+"' alt='' class='sis'/> "
				+" <p class='sdWordA moreWord'>"+itemIn.productName+"</p> "
				+" <p class='sdWordP'>￥ "+price01.toFixed(2)+"</p> "
				+" <p class='sdWordC'> "
				//		<span>颜色: 天蓝色</span>
				//		<span>尺码: 165/s</span>
						+" 		<span>"+goods_sku_info+"</span> "
					+" </p> "
					+"<span class='mWord'>x "+itemIn.quantity+"</span>"
				+" </a>"
			//	alert("payStatus:"+payStatus+":shipStatus:"+shipStatus+":isComment:"+isComment+":isValid:"+isValid)
				if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && itemIn.isComment=='0' && isValid=='0'){
					//alert("payStatus:"+payStatus+":shipStatus:"+shipStatus+":isComment:"+isComment+":isValid:"+isValid)
					optionHtml+= " <a href='"+commentUrl+"' class='comment'>评价</a>"
				}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && itemIn.isComment=='1' && isValid=='0'){
					if(isCommentCount=='1'){
						optionHtml+=  " <a href='"+addCommentUrl+"' class='comment'>追评</a>"
					}else if(isCommentCount=='2'){
						
					}
				}	
		optionHtml+= " </div>";
	});
	return optionHtml;
};

/**创建按钮信息***/
function createByBtn(item,itemIn,type){
	var str='';
	var status='';
	var statusDetail='';
	var comment='';
	var payStatus = item.payStatus;
	var shipStatus = item.shipStatus;
	var isComment = item.isComment;
	var isValid = item.isValid;
	var detailUrl = rootPath + "/order/orderData?orderId="+item.orderId; //订单详情页
	var commentUrl = rootPath + "/order/evaluate?orderId="+item.orderId+"&goodsId=14"; //订单评价
	var addCommentUrl = rootPath + "/order/addEvaluate?orderId="+item.orderId+"&goodsId=14"; //订单追加评价
	var orderCardsPayUrl = rootPath + "/order/order_card_pay?orderId="+item.orderId; //订单追加评价
	//var expressUrl ='https://m.kuaidi100.com/index_all.html?type='+item.expressName+'&postid=' + item.expressCode;
	var informationUrl = rootPath +'/member/information?expressName='+item.expressName+'&expressCode=' + item.expressCode+'&orderId='+item.orderId+'&newsTitle=物流信息';
//	var delOrderUrl = rootPath + "/order/cancelOrDeleteOrder?status=1&orderId="+item.orderId; //删除订单 status 0:取消订单 ;1：删除订单
//	var cancelOrderUrl = rootPath + "/order/cancelOrDeleteOrder?status=0&orderId="+item.orderId; //取消订单 status 0:取消订单 ;1：删除订单
	var $unitPrice ="";
	if(itemIn.discountMarketPrice){
		$unitPrice = '<div><span>￥'+itemIn.discountMarketPrice+'</span></div>';
	}else{
		$unitPrice = '<div><span>￥'+itemIn.marketPrice+'</span></div>';
	}
	
	if(payStatus == '3' && shipStatus =='0' && isComment=='0' && isValid=='0'){
		statusDetail ='等待买家付款'; //onclick=cancelOrDeleteOrder('+item.orderId+","+"0"+')
		status='<p><a href="'+detailUrl+'" target="_blank">订单详情</a> '
			+" <a href='javascript:void(0);' onclick='cancelOrDeleteOrder"+'("'+item.orderId+'","0")'+"' orderId='"+item.orderId+"' status='0'  class='cacelOrdelOrder'  target='_self'>取消订单</a>"
			+' <a href="'+orderCardsPayUrl+'" target="_blank" >付款</a></p>';
	}else if((payStatus == '1' || payStatus =='6')  && shipStatus =='0' && isComment=='0' && isValid=='0'){
		statusDetail ='等待卖家发货';
	//	status='<div><!--<div>'+'等待卖家发货'+'</div>--><a href="'+detailUrl+'" target="_blank">订单详情</a><a href="" target="_blank">提醒发货</a></div>';
		status='<p><a href="'+detailUrl+'" target="_blank">订单详情</a></p>';
	}else if((payStatus == '1' || payStatus =='6') &&( shipStatus =='1' || shipStatus == '2')&& isComment=='0' && isValid=='0'){
		statusDetail ='卖家已发货';
		status='<p><a href="'+detailUrl+'" target="_blank">订单详情</a><a href="'+informationUrl+'" target="_blank">查看物流</a><a href="javascript:void(0);"  onclick="confirmReceiptOrder(\''+item.orderId+'\')" orderId="'+item.orderId+'"  target="_blank">确认收货</a></p>';
	}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='0' && isValid=='0'){
		statusDetail ='交易成功';
		status='<p><a href="'+detailUrl+'" target="_blank">订单详情</a> '
			+" <a href='javascript:void(0);' orderId='"+item.orderId+"' status='1' onclick='cancelOrDeleteOrder"+'("'+item.orderId+'","1")'+"' class='cacelOrdelOrder' target='_self'>删除订单</a>"
			+ '<a href="'+informationUrl+'" target="_blank">查看物流</a>'
			// <a href="'+commentUrl+'">评价</a>'
			+'</p>';
		//	comment ='<div class="comment"><a href="'+commentUrl+'" target="_blank">评价</a></div>';
	}else if((payStatus == '1' || payStatus =='6') && shipStatus =='5' && isComment=='1' && isValid=='0'){
		statusDetail ='交易成功';
		status='<p><a href="'+detailUrl+'"  target="_blank">订单详情</a>'
			+" <a href='javascript:void(0);' onclick='cancelOrDeleteOrder"+'("'+item.orderId+'","1")'+"' orderId='"+item.orderId+"' status='1'   target='_self'>删除订单</a></p>" 
	//		+' <a href="'+expressUrl+'" target="_blank">查看物流</a><a href="'+addCommentUrl+'" target="_blank">追加评价</a>';
	//	comment ='<div class="comment"><a href="'+addCommentUrl+'" target="_blank">追加评价</a></div>';
	}else if(payStatus == '3' && shipStatus =='0' && isComment=='0' && isValid=='1'){
		statusDetail ='交易关闭';
		status='<p><a href="'+detailUrl+'" target="_blank">订单详情</a>'
		+" <a href='javascript:void(0);' onclick='cancelOrDeleteOrder"+'("'+item.orderId+'","1")'+"' orderId='"+item.orderId+"' status='1'  target='_self'>删除订单</a></p>";
	}
	if(type=="1"){
		return statusDetail;
	}else if(type=="2"){
		return status;
	}
	return null;
}

/****
 * 点击切换样式
 */
function addClass(queryStatus){
	$('.orders .myOrders a').removeClass("oneA");
	$('.orders .myOrders a').each(function () {
		var dataId = $(this).attr("data-id");
		if(dataId==queryStatus){
			$(this).prop("class", "oneA")
		}
	});
}